package com.czk.scope;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.Scope;

/**
 * 自定义作用域
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/28 15:22
 */
public class ThreadScope implements Scope {
    private static final ThreadLocal THREAD_LOCAL_SCOPE = new ThreadLocal();

    @Override
    public Object get(String s, ObjectFactory<?> objectFactory) {
        // 拿到当前线程绑定的对象
        Object o = THREAD_LOCAL_SCOPE.get();
        if (o != null) {
            // fanhui
            return o;
        }
        o = objectFactory.getObject();
        // 加入threadLocal
        THREAD_LOCAL_SCOPE.set(o);
        return o;
    }

    @Override
    public Object remove(String s) {
        THREAD_LOCAL_SCOPE.remove();
        return null;
    }

    @Override
    public void registerDestructionCallback(String s, Runnable runnable) {

    }

    @Override
    public Object resolveContextualObject(String s) {
        return null;
    }

    @Override
    public String getConversationId() {
        return null;
    }
}
